package com.crazy.gridview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("GridView");
        GridView gv=(GridView) findViewById(R.id.grid_view);
        gv.setAdapter(new ImageAdapter(this));
    }

    public class ImageAdapter extends BaseAdapter{

        private Context mContext;

        public  ImageAdapter(Context c){
            mContext=c;
        }

        private Integer[] mThumdIds={
                R.drawable.gv01,R.drawable.gv02,
                R.drawable.gv03,R.drawable.gv04,
                R.drawable.gv05,R.drawable.gv06,
                R.drawable.gv07,R.drawable.gv08,
                R.drawable.gv09
        };

        public int getCount(){
            return mThumdIds.length;
        }

        public Object getItem(int arg0){
            return null;
        }

        @Override
        public long getItemId(int arg0){
            return 0;
        }

        @Override
        public View getView(int position,View convertView,ViewGroup parent){
            ImageView imageView;
            if (convertView==null){
                imageView=new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(180,180));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(10,10,10,10);
            }else{
                imageView=(ImageView) convertView;
            }

            imageView.setImageResource(mThumdIds[position]);
            return imageView;
        }
    }
}
